(function(){
    "use strict";
    app.controller('warehouseCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','LANGUAGE',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,LANGUAGE) {
            $rootScope.moduleTitle = '资源管理 > 仓库';//定义当前页
            // $rootScope.link = '/statics/css/user.css';//引入页面样式
            $rootScope.link = '/statics/css/tpl-card.css';
            $scope.param = {
                rows: 10
            };
            $scope.isbatchDelete = true;
            //获取主机列表
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows,
                        registryName: $scope.searchByname
                    };
                httpLoad.loadData({
                    url:'/runtime/registry/list',
                    data: params,
                    success:function(data){
                        if(data.rows&&data.rows.length!=0){
                            $scope.warehouseList = data.rows;
                            $scope.totalCount = data.total;
                            $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };
            $scope.getData(1);

            //所含镜像
            $scope.imgcontain = function(item){
                $state.go('app.containerManger.img',{id:item.id,flag:0});
            };

            //新增
            $scope.add = function(){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/resources/warehouse/add.html',
                    controller : 'addWarehouseModalCtrl'// 初始化模态范围
                });
                modalInstance.result.then(function(data){
                    $scope.getData();
                },function(){});
            };
            //编辑
            $scope.update = function(item){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/resources/warehouse/update.html',  //指向上面创建的视图
                    controller : 'updateWarehouseModalCtrl',// 初始化模态范围
                    resolve : {
                        itemData: function() {
                            return item;
                        }
                    }
                });
                modalInstance.result.then(function(data){
                    $scope.getData();
                },function(){});
            };
            //删除
            $scope.remove = function(id){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/resources/warehouse/delete.html',
                    controller : 'deleteWarehouseModalCtrl',
                    resolve : {
                        id: function() {
                            return  id;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
            //导入
            $scope.uploadImg = function(id){
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/resources/warehouse/uploadImg.html',
                    controller : 'uploadImgModalCtrl',// 初始化模态范围
                    resolve : {
                        id: function() {
                            return id;
                        }
                    }
                });
                modalInstance.result.then(function(){
                },function(){});
            }
        }
    ]);
    //新增ctrl
    angular.module('app').controller('addWarehouseModalCtrl',['$scope','$modalInstance','LANGUAGE','httpLoad',
        function($scope,$modalInstance,LANGUAGE,httpLoad){
            $scope.haEnable = 0;
            httpLoad.loadData({
                url:'/runtime/host/unUsedHosts',
                data: {hostType: 2
                },
                success:function(data){
                    if(data.success&&data.data.length!=0){
                        $scope.types = data.data;
                        $scope.type = $scope.types[0].hostId;
                        $scope.standbys = data.data;
                        $scope.standby = $scope.standbys[0].hostId;
                    }
                }
            });
            $scope.isHigh = function () {
                if($('#highId').is(':checked')){
                    $scope.haEnable = 1;
                    $("#showHigh").show();
                }else{
                    $scope.haEnable = 0;
                    $("#showHigh").hide();
                }
            };
            $scope.ok = function(){
                if($scope.type==$scope.standby){
                    $scope.pop("仓库主机与备用主机一致","error");
                    return false;
                }
                if($scope.haEnable==0){
                    $scope.addData = {
                        haEnable:$scope.haEnable,
                        serviceIp: $scope.serviceIp,
                        hostId:$scope.type,
                        registryName:$scope.registryName
                    };
                }else{
                    $scope.addData = {
                        haEnable:$scope.haEnable,
                        serviceIp: $scope.serviceIp,
                        hostId:$scope.type+","+$scope.standby,
                        registryName:$scope.registryName
                    };
                };
                httpLoad.loadData({
                    url:'/runtime/registry/create',
                    data: $scope.addData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.addData);
                        }else{
                            $scope.pop(data.message,"error");
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //编辑ctrl
    angular.module('app').controller('updateWarehouseModalCtrl',['$scope','$modalInstance','itemData','LANGUAGE','httpLoad',
        function($scope,$modalInstance,itemData,LANGUAGE,httpLoad){
            $scope.updateData = {};
            $scope.updateData.registryName = itemData.registryName;
            $scope.ok = function(){
                $scope.updateData.registryId = itemData.registryId;
                httpLoad.loadData({
                    url:'/runtime/registry/modify',
                    data: $scope.updateData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.updateData);
                        }else{
                            $scope.pop(data.message,"error");
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //删除ctrl
    angular.module('app').controller('deleteWarehouseModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad){ //依赖于modalInstance
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/runtime/registry/remove',
                    data:{registryId: id},
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                            $scope.pop(data.message);
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
    //导入ctrl
    angular.module('app').controller('uploadImgModalCtrl',['$rootScope','$scope','$modalInstance','LANGUAGE','httpLoad','id',
        function($rootScope,$scope,$modalInstance,LANGUAGE,httpLoad,id){
            $scope.websocketUrl='/uploadService';
            $scope.id=id;
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
})();